package matchthreegame.utils;

import java.util.ArrayList;

import com.smartfoxserver.v2.entities.User;

public class WaitingList {
    ArrayList<User> users;
    private static final Object lock = new Object();

    public WaitingList() {
    	users = new ArrayList<User>();
    }

    public WaitingList(ArrayList<User> aUsers) {
            this.users = aUsers;
    }

    public void addUser(User aUser) {
    	synchronized(lock){
    			if (!this.contains(aUser.getId())) {
            		users.add(aUser);
            	}
    		}
            
    }

    public boolean contains(Integer userId) {
    	synchronized(lock){
    		boolean contains = false;

            for (User currentUser : this.users) {
                    if (currentUser.getId() == userId.intValue() ) {
                            contains = true;
                    }
            }

            return contains;
    	} 
    }

    public User remove(Integer aUserId) {
    	synchronized(lock){
            User userToRemove = null;
            if ( this.users.size() > 0 ) {
            	 for (User currentUser : this.users) {
                 	if (currentUser.getId() == aUserId.intValue() ) {
                 			userToRemove = currentUser;
                              break;
                     }
                 }

                 if (userToRemove != null) {
                 	this.users.remove(userToRemove);
                 }
            }

            return userToRemove;
    	}
    }

//    public void sort() {
//            Collections.sort(this.Users, new WaitingListSorter());
//    }

    public ArrayList<User> getUsers() {
    	synchronized(lock){
            return this.users;
    	}
    }

    public Integer getNumberWaiting() {
    	synchronized(lock){
    		 return this.users.size();
    	} 
    }

}
